package cn.sprouting.tool;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;

/**
 * 随机文本
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2021/11/5 9:17
 */
@Slf4j
public class RandomTextTool {


    /**
     * 随机生成一个汉字
     * @return
     */
    public static String getChinese(){
        int highCode = RandomUtil.randomInt(176, 215);
        int lowCode = RandomUtil.randomInt(161, 254);
        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(highCode)).byteValue();
        b[1] = (Integer.valueOf(lowCode)).byteValue();
        String chinese = "";
        try {
            chinese = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
            log.error("发生编码解析异常", e);
        }

        return chinese;
    }

    /**
     * 随机生成N个汉字
     * @param num 生成汉字的数量
     * @return
     */
    public static String getChinese(Integer num){
        if (num == null || num < 1){
            num = 1;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num; i ++){
            String chinese = getChinese();
            sb.append(chinese);
        }

        return sb.toString();
    }

}
